package com.shycloud.mido.opter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.member.api.entity.BasicGoodsInfo;
import com.shycloud.mido.opter.api.entity.StockInOrderInfo;
import com.shycloud.mido.opter.api.entity.StockWarehouseDetail;
import com.shycloud.mido.opter.api.vo.StockInOrderListVO;
import com.shycloud.mido.opter.api.vo.StockInOrderVO;
import com.shycloud.mido.opter.mapper.BasicGoodsInfoMapper;
import com.shycloud.mido.opter.mapper.StockInOrderInfoMapper;
import com.shycloud.mido.opter.mapper.StockWarehouseDetailMapper;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.opter.api.entity.StockInOrder;
import com.shycloud.mido.opter.mapper.StockInOrderMapper;
import com.shycloud.mido.opter.service.StockInOrderService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class StockInOrderServiceImpl extends ServiceImpl<StockInOrderMapper, StockInOrder> implements StockInOrderService {

	/** 入库订单模块 mapper */
	private StockInOrderMapper inOrderMapper;
	/** 入库订单模块商品详情模块 mapper */
	private StockInOrderInfoMapper inOrderInfoMapper;
	/** 商品模块 mapper */
	private BasicGoodsInfoMapper goodsMapper;
	/** 库存详情模块 mapper */
	private StockWarehouseDetailMapper warehouseDetailMapper;

	/**
	 * 新增入库订单
	 *
	 * @param stockInOrderVO
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/9/22 17:40
	 **/
	@SneakyThrows
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public Boolean saveInOrder(StockInOrderVO stockInOrderVO) {

		StockInOrder inOrder = new StockInOrder();

		BeanUtils.copyProperties(stockInOrderVO, inOrder);

		String dateStr = stockInOrderVO.getDate();

		Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
		inOrder.setDeliveryDate(date);
		//新增操作
		saveOrUpdate(inOrder);

		//循环操作新增商品详情信息
		List<Map<String, Integer>> goodsInfo = stockInOrderVO.getGoodsInfo();

		//先移除后新增
		inOrderInfoMapper.delete(new QueryWrapper<StockInOrderInfo>().eq(StockInOrderInfo.COL_IN_ORDER_ID, inOrder.getId()));

		for (Map<String, Integer> goodsInfoMap : goodsInfo) {

			//*****************************************新增入库商品详情**************************************************//
			//商品ID
			Integer goodsId = goodsInfoMap.get("goodsId");
			//数量
			Integer goodsCount = goodsInfoMap.get("goodsCount");
			//商品详情信息
			StockInOrderInfo inOrderInfo = new StockInOrderInfo(inOrder.getId(), goodsId, goodsCount);
			//新增
			inOrderInfoMapper.insert(inOrderInfo);

			//*****************************************新增商品剩余数量**************************************************//
			BasicGoodsInfo basicGoodsInfo = goodsMapper.selectById(goodsId);
			//更新库存
			Integer updGoodsCount = (basicGoodsInfo.getStockQuantity() == null ? 0 : basicGoodsInfo.getStockQuantity()) + goodsCount;
			basicGoodsInfo.setStockQuantity(updGoodsCount);
			goodsMapper.updateById(basicGoodsInfo);

			//*****************************************添加仓库库存*****************************************************//
			List<StockWarehouseDetail> stockWarehouseDetails = warehouseDetailMapper.selectList(new QueryWrapper<StockWarehouseDetail>().eq("warehouse_id", inOrder.getWarehouseId()));

			Boolean flg = false;

			for (StockWarehouseDetail stockWarehouseDetail : stockWarehouseDetails) {

				if (stockWarehouseDetail.getGoodsId() == goodsId) {
					//累计入库数量
					stockWarehouseDetail.setAccuInAmount((stockWarehouseDetail.getAccuInAmount() == null ? 0 : stockWarehouseDetail.getAccuInAmount()) + goodsCount);
					//当前商品库存
					stockWarehouseDetail.setGoodsAmount((stockWarehouseDetail.getGoodsAmount() == null ? 0 : stockWarehouseDetail.getGoodsAmount()) + goodsCount);
					warehouseDetailMapper.updateById(stockWarehouseDetail);
					flg = true;
					break;
				}

			}
			//如果仓库没有此商品，则新增商品
			if (!flg) {
				StockWarehouseDetail stockWarehouseDetail = new StockWarehouseDetail();
				//入库数量
				stockWarehouseDetail.setAccuInAmount(goodsCount);
				//出库数量
				stockWarehouseDetail.setAccuOutAmount(0);
				//仓库商品数量
				stockWarehouseDetail.setGoodsAmount(goodsCount);
				//仓库 ID
				stockWarehouseDetail.setWarehouseId(inOrder.getWarehouseId());
				//商品 ID
				stockWarehouseDetail.setGoodsId(goodsId);
				//新增
				warehouseDetailMapper.insert(stockWarehouseDetail);
			}

		}

		return true;
	}


	/**
	 * 查询入库订单
	 *
	 * @param supplierName
	 * @param startDate
	 * @param endDate
	 * @param page
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.shycloud.mido.opter.api.vo.StockInOrderListVO>
	 * @author nianhua.jiang
	 * @date 2020/9/23 16:39
	 **/
	@Override
	public IPage<StockInOrderListVO> getInOrderList(String supplierName, String startDate, String endDate, Page page, String isCommit) {

		IPage<StockInOrderListVO> inOrderList = inOrderMapper.getInOrderPage(page, supplierName, startDate, endDate, isCommit);

		List<StockInOrderListVO> records = inOrderList.getRecords();

		for (StockInOrderListVO record : records) {
			Integer count = inOrderInfoMapper.getInOrderGoodsCount(record.getId());
			record.setCount(count);

			//插入商品详情
			record.setGoodsInfo(inOrderInfoMapper.getInOrderGoodsInfo(record.getId()));

		}

		return inOrderList;
	}
}
